<template>
  <div class="filters">
    <el-row type="flex" class="filters-top" justify="space-between" align="middle">
      <el-col :span="8">
        单程：
        {{data.info.departCity}} - {{data.info.destCity}}
        /
        {{data.info.departDate}}
      </el-col>
      <el-col :span="4">
        <el-select size="mini" v-model="airport" placeholder="起飞机场" @change="handleAirport">
          <el-option
            v-for="(item, index) in data.options.airport"
            :key="index"
            :label="item"
            :value="item"
          ></el-option>
        </el-select>
      </el-col>
      <el-col :span="4">
        <el-select size="mini" v-model="flightTimes" placeholder="起飞时间" @change="handleFlightTimes">
          <el-option
            v-for="(item, index) in data.options.flightTimes"
            :key="index"
            :label="`${item.from}:00 - ${item.to}:00`"
            :value="`${item.from}, ${item.to}`"
          ></el-option>
        </el-select>
      </el-col>
      <el-col :span="4">
        <el-select size="mini" v-model="company" placeholder="航空公司" @change="handleCompany">
          <el-option
            v-for="(item, index) in data.options.company"
            :key="index"
            :label="item"
            :value="item"
          ></el-option>
        </el-select>
      </el-col>
      <el-col :span="4">
        <el-select size="mini" v-model="airSize" placeholder="机型" @change="handleAirSize">
          <el-option
            v-for="(item, index) in  planesize"
            :key="index"
            :label="item.labrl"
            :value="item.value"
          ></el-option>
        </el-select>
      </el-col>
    </el-row>
    <div class="filter-cancel">
      筛选：
      <el-button type="primary" round plain size="mini" @click="handleFiltersCancel">撤销</el-button>
    </div>
  </div>
</template>
<script>
export default {
  props: {
    data: Object,
    flights: Array
  },
  data() {
    return {
      airport: "", // 机场
      flightTimes: "", // 出发时间
      company: "", // 航空公司
      airSize: "", // 机型大小
      planesize: [
        { labrl: "大", value: "L" },
        { labrl: "中", value: "M" },
        { labrl: "小", value: "S" }
      ]
    };
  },
  methods: {
    // 选择机场时候触发
    handleAirport(value) {
      const newdislist = this.flights.filter(el => {
        return el.org_airport_name == value;
      });
      this.$emit("setflights", newdislist);
    },

    // 选择出发时间时候触发
    handleFlightTimes(value) {
      const newdislist = this.flights.filter(el => {
        let dep_time = el.dep_time.split(":");
        let calueArr = value.split(",");
        return (
          Number(dep_time[0]) > Number(calueArr[0]) &&
          Number(dep_time[0]) < Number(calueArr[1])
        );
      });
      this.$emit("setflights", newdislist);
    },

    // 选择航空公司时候触发
    handleCompany(value) {
      const newdislist = this.flights.filter(el => {
        return el.airline_name == value;
      });
      this.$emit("setflights", newdislist);
    },

    // 选择机型时候触发
    handleAirSize(value) {
      const newdislist = this.flights.filter(el => {
        return el.plane_size == value;
      });
      console.log(value);
      this.$emit("setflights", newdislist);
    },

    // 撤销条件时候触发
    handleFiltersCancel() {
      this.airport = "";
      this.flightTimes = "";
      this.company = "";
      this.airSize = "";

      this.$emit("setflights", this.data.flights);
    }
  }
};
</script>

<style scoped lang="less">
.filters {
  margin-bottom: 20px;
}

.filters-top {
  > div {
    /deep/ .el-select {
      margin-left: 10px;
    }
  }
}

.filter-cancel {
  margin-top: 10px;
}
</style>